package cpgears {
	import asunit.framework.TestCase;
	import cpgears.movie.MovieMock;
	import cpgears.properties.WidgetPropertiesBase;
	import cpgears.cpgearsInternal;
	import cpgears.movie.IMovie;
	
	use namespace cpgearsInternal;
	
	/**
	 * ...
	 * @author Whyves
	 */
	public class BaseWidgetImplTest extends TestCase{
		
		private var mWidget:WidgetBase = null;
		
		public function BaseWidgetImplTest(testMethod:String = null) {
			super(testMethod);
		}
		
		//---------------------------------------------------------------------
		//  showPreview
		//---------------------------------------------------------------------
		public function testshowPreviewWidgetInstantiation():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("showPreview", functionCalled);
			}
			addChild(createWidget(callback));
		}

		public function testshowPreview():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("showPreview", functionCalled);
			}
			var widget:BaseImplMock = createWidget(callback);
			widget.cpSetValue("widgetMode", "Preview");
			widget.setParameters( { } );
		}
		
		public function testshowPreviewWithParams():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("showPreview", functionCalled);
				assertEquals("OK", properties["param1"]);
			}
			var widget:BaseImplMock = createWidget(callback);
			widget.cpSetValue("widgetMode", "Preview");
			widget.setParameters({param1:"OK"});
		}
		
		//---------------------------------------------------------------------
		//  enterEdit
		//---------------------------------------------------------------------
		public function testenterEditCp4FirstInstantiation():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("enterEdit", functionCalled);
			}
			var widget:BaseImplMock = createWidget(callback);
			widget.cpSetValue("widgetMode", "Edit");
		}
		
		public function testenterEdit():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("enterEdit", functionCalled);
			}
			var widget:BaseImplMock = createWidget(callback);
			widget.cpSetValue("widgetMode", "Edit");
			widget.setInspectorParameters({});
		}
		
		public function testenterEditWithParams():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("enterEdit", functionCalled);
				assertEquals("OK", properties["param1"]);
			}
			var widget:BaseImplMock = createWidget(callback);
			widget.cpSetValue("widgetMode", "Edit");
			widget.setInspectorParameters({param1:"OK"});
		}
		
		//---------------------------------------------------------------------
		//  Properties
		//---------------------------------------------------------------------
		public function testGetOwnProperties():void {
			var properties:WidgetPropertiesBase = new WidgetPropertiesBase(prototype);
			properties["param1"] = "OK";
			var widget:WidgetBase = new WidgetBase(properties);
			var propertiesObject:Object = widget.getInspectorParameters();
			assertEquals("OK", propertiesObject["param1"]);
		}
		
		public function testGetDefaultPropertiesObject():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("showPreview", functionCalled);
				assertTrue(properties is WidgetPropertiesBase);
			}
			addChild(createWidget(callback));
		}
		//---------------------------------------------------------------------
		//  enterStage
		//---------------------------------------------------------------------
		public function testenterStage():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("enterStage", functionCalled);
			}
			var widget:BaseImplMock = createWidget(callback);
			widget.cpSetValue("widgetMode", "Stage");
			widget.setParameters({});
		}
		
		public function testenterStageWithParams():void {
			function callback(functionCalled:String, properties:Object):void {
				assertEquals("enterStage", functionCalled);
				assertEquals("OK", properties["param1"]);
			}
			var widget:BaseImplMock = createWidget(callback);
			widget.cpSetValue("widgetMode", "Stage");
			widget.setParameters({param1:"OK"});
		}
		
		//---------------------------------------------------------------------
		//  enterRuntime
		//---------------------------------------------------------------------
		public function testenterRuntime():void {
			function callback(functionCalled:String, properties:Object, movie:IMovie):void {
				assertEquals("enterRuntime", functionCalled);
			}
			var widget:BaseImplMock = createWidget(callback);
			widget.cpSetValue("movieHandle", new MovieMock());
		}

		public function testOnEnterRuntimWithParams():void {
			function callback(functionCalled:String, properties:Object, movie:IMovie):void {
				assertEquals("enterRuntime", functionCalled);
				assertEquals("OK", properties["param1"]);
			}
			var widget:BaseImplMock = createWidget(callback);
			var props:WidgetPropertiesBase = new WidgetPropertiesBase(prototype);
			props.param1 = "OK"
			widget.cpSetValue("movieHandle",  new MovieMock(props));
		}

		//---------------------------------------------------------------------
		//  IsReadyForSnapshot
		//---------------------------------------------------------------------
		public function testDefaultIsReadyForSnapshot():void {
			var widget:WidgetBase = new WidgetBase();
			assertTrue(widget.IsReadyForSnapShot());
		}
		
		//---------------------------------------------------------------------
		//
		//  Helper functions
		//
		//---------------------------------------------------------------------
		
		private function createWidget(callback:Function):BaseImplMock {
			var handler:Function = addAsync(callback, 500);
			return new BaseImplMock(handler);
		}
	}
}